﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using BL;
using System.IO;
using Entities;


namespace WindowsFormsApplication1
{
    public partial class frmthongtin : Form
    {
        CongNhanBL congNhanBL;
        BindingSource bs = new BindingSource();
        DataTable dtCongNhan=null;
        DataSet dsCongNhan;

        KhachHangBL khachHangBL;
        BindingSource bskh = new BindingSource();
        DataTable dtKhachHang=null;
        DataSet dsKhachHang ;

        DonHangBL donHangBL;
        BindingSource bsdh = new BindingSource();
        DataTable dtDonHang = null;
        DataSet dsDonHang;

        ThongTinDonHangBL thongTinDonHangBL;
        BindingSource bsttdh = new BindingSource();
        DataTable dtThongTinDonHang = null;
        DataSet dsThongTinDonHang;

        SanPhamBL sanPhamBL;
        BindingSource bssp = new BindingSource();
        DataTable dtSanPham = null;
        DataSet dsSanPham;
        int sanPhamMoi;

        public frmthongtin()
        {
            InitializeComponent();

            congNhanBL = new CongNhanBL();
            khachHangBL = new KhachHangBL();
            donHangBL = new DonHangBL();
            thongTinDonHangBL = new ThongTinDonHangBL();
            sanPhamBL = new SanPhamBL();
        }

        private void frmthongtin_load(object sender, EventArgs e )
        { 
        
        }

        private void btnDanhSachCongNhan_Click(object sender, EventArgs e)
        {
            try
            {
                // bs.Add();
                dgvcongnhan1.AutoGenerateColumns = false;

                dsCongNhan = congNhanBL.layDanhSachCongNhan();
                //dgvcongnhan1.DataSource = ketnoi.ex("select * from CongNhan");
                bs.DataSource = dsCongNhan.Tables[0];
                dgvcongnhan1.DataSource = bs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
                            
         //   dgvcongnhan1.DataBindings.Add("MaCongNhan", bs, "MaCongNhan");
         //  dgvcongnhan1.DataBindings.Add("TenCongNhan", bs, "TenCongNhan");
         // dgvcongnhan1.DataBindings.Add("BacCongNhan", bs, "BacCongNhan");
        }

        private void tabPage4_Click(object sender, EventArgs e)
        {
            
        }
        private void btnTimKiemCongNhan_Click(object sender, EventArgs e)
        {
            try
            {
                int loai;

                if (rbtnMaCongNhan.Checked)
                {
                    loai = 1;
                }
                else if (rbtnTenCongNhan.Checked)
                {
                    loai = 2;
                }
                else
                {
                    loai = 3;
                }

                dsCongNhan = congNhanBL.timKiemCongNhan(loai, txtTimKiemCongNhan.Text);

                dgvcongnhan1.DataSource = dsCongNhan.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }           
        }

        private void dgvcongnhan1_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaPhongSanXuat() == true)
                {
                    // MessageBox.Show("valid");
                    bs.EndEdit();
                    dgvDonHangChiTiet.EndEdit();
                    congNhanBL.updateCongNhan(dsCongNhan.Tables[0]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDanhSachKhachHang_Click(object sender, EventArgs e)
        {
            try
            {
                dgvkhachhang.AutoGenerateColumns = false;
                dsKhachHang = khachHangBL.layDanhSachKhachHang();
                bskh.DataSource = dsKhachHang.Tables[0];
                dgvkhachhang.DataSource = bskh;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnTimKiemKhachHang_Click(object sender, EventArgs e)
        {
            try
            {
                int loai;

                if (rbtnMaKhachHang.Checked)
                {
                    loai = 1;
                }
                else if (rbtnTenKhachHang.Checked)
                {
                    loai = 2;
                }
                else if (rbtnDiaChiLienLac.Checked)
                {
                    loai = 3;
                }
                else if (rbtnSoDienThoai.Checked)
                {
                    loai = 4;
                }
                else
                {
                    loai = 5;
                }
                dsKhachHang = khachHangBL.timKiemKhachHang(loai, txtTimKiemKhachHang.Text);

                dgvkhachhang.DataSource = dsKhachHang.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        private void dgvkhachhang_RowValidated_1(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                //MessageBox.Show("valid");
                bskh.EndEdit();
                dgvDonHangChiTiet.EndEdit();
                khachHangBL.updateKhachHang(dsKhachHang.Tables[0]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void frmthongtin_Load_1(object sender, EventArgs e)
        {
            try
            {
                DataTable dtSanPham = sanPhamBL.layDanhSachSanPham().Tables[0];
                cmbTimKiemMaSanPham.DataSource = dtSanPham;
                cmbTimKiemMaSanPham.ValueMember = "MaSanPham";
                cmbTimKiemMaSanPham.DisplayMember = "MaSanPham";

                cmbTimKiemMaSanPham.AutoCompleteSource = AutoCompleteSource.ListItems;

                dsThongTinDonHang = thongTinDonHangBL.timKiemThongTinDonHang(1, "-1");
                bsttdh.DataSource = dsThongTinDonHang.Tables[0];
                dgvDonHang.DataSource = bsttdh;

                dsDonHang = donHangBL.timKiemDonHang(1, "-1");
                bsdh.DataSource = dsDonHang.Tables[0];
                dgvDonHangChiTiet.DataSource = bsdh;


                dsKhachHang = khachHangBL.timKiemKhachHang(1, "-1");
                bskh.DataSource = dsKhachHang.Tables[0];
                dgvkhachhang.DataSource = bskh;

                timKiemSanPham(cmbTimKiemMaSanPham.SelectedValue.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }           
        }

        private void btnTimKiemDonHang_Click(object sender, EventArgs e)
        {
            try
            {
                bool goiBangKhachHang = false;
                int loai = -1;

                if (rbtnMaDonHang.Checked)
                {
                    loai = 1;
                }
                else if (rbtnMaKhachHang1.Checked)
                {
                    loai = 1;
                    goiBangKhachHang = true;
                }
                else if (rbtnNgayNhan.Checked)
                {
                    loai = 2;
                    goiBangKhachHang = true;
                }
                else if (rbtnNgayGiao.Checked)
                {
                    loai = 3;
                    goiBangKhachHang = true;
                }
                dgvDonHangChiTiet.AutoGenerateColumns = false;
                dgvDonHang.AutoGenerateColumns = false;


               // if (!goiBangKhachHang)
                //{
                    dsDonHang = donHangBL.timKiemDonHang(loai, txtTimKiemDonHang.Text);
                    bsdh.DataSource = dsDonHang.Tables[0];
                    dgvDonHangChiTiet.DataSource = bsdh;
              //  }
              //  else
              //  {
                    dsThongTinDonHang = thongTinDonHangBL.timKiemThongTinDonHang(loai, txtTimKiemDonHang.Text);
                    bsttdh.DataSource = dsThongTinDonHang.Tables[0];
                    dgvDonHang.DataSource = bsttdh;
             //   }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dgvDonHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (!rbtnMaDonHang.Checked)
                {
                    dsDonHang = donHangBL.timKiemDonHang(1, dgvDonHang.Rows[e.RowIndex].Cells["MaDonHang"].Value.ToString());
                    dgvDonHangChiTiet.DataSource = dsDonHang.Tables[0];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnTimKiemSanPham_Click(object sender, EventArgs e)
        {
            try
            {
                //txtMaNapChup.DataBindings.Clear();
                //txtTenSanPham.DataBindings.Clear();
                //txtSoMau.DataBindings.Clear();
                //txtKichThuoc.DataBindings.Clear();
                //txtMaRap.DataBindings.Clear();
                //txtMaKhuon.DataBindings.Clear();
                //txtDoKho.DataBindings.Clear();

                //dsSanPham = sanPhamBL.timKiemSanPham(cmbTimKiemMaSanPham.SelectedValue.ToString());
                //dtSanPham = dsSanPham.Tables[0];
                //bssp.DataSource = dtSanPham;
                //txtMaSanPham.DataBindings.Add("Text", bssp, "MaSanPham");
                //txtMaNapChup.DataBindings.Add("Text", bssp, "MaNapChup");
                //txtTenSanPham.DataBindings.Add("Text", bssp, "TenSanPham");
                //txtSoMau.DataBindings.Add("Text", bssp, "SoMau");
                ////txtSoVien.DataBindings.Add("Text", bssp, "SoVien");
                //txtKichThuoc.DataBindings.Add("Text", bssp, "KichThuoc");
                //txtMaRap.DataBindings.Add("Text", bssp, "MaRap");
                //txtMaKhuon.DataBindings.Add("Text", bssp, "Makhuon");
                //txtDoKho.DataBindings.Add("Text", bssp, "DoKho");
                //picHinhVe.Image = Image.FromFile(dsSanPham.Tables[0].Rows[0]["HinhVe"].ToString());
                timKiemSanPham(cmbTimKiemMaSanPham.SelectedValue.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }                      
        }

        private void timKiemSanPham(String maSanPham)
        {
            txtMaSanPham.DataBindings.Clear();
            txtMaNapChup.DataBindings.Clear();
            txtTenSanPham.DataBindings.Clear();
            txtSoMau.DataBindings.Clear();
            txtKichThuoc.DataBindings.Clear();
            txtMaRap.DataBindings.Clear();
            txtMaKhuon.DataBindings.Clear();
            txtDoKho.DataBindings.Clear();
            txtHinhVe.DataBindings.Clear();

            dsSanPham = sanPhamBL.timKiemSanPham(maSanPham);
            dtSanPham = dsSanPham.Tables[0];
            bssp.DataSource = dtSanPham;
            txtMaSanPham.DataBindings.Add("Text", bssp, "MaSanPham");
            txtMaNapChup.DataBindings.Add("Text", bssp, "MaNapChup");
            txtTenSanPham.DataBindings.Add("Text", bssp, "TenSanPham");
            txtSoMau.DataBindings.Add("Text", bssp, "SoMau");
            //txtSoVien.DataBindings.Add("Text", bssp, "SoVien");
            txtKichThuoc.DataBindings.Add("Text", bssp, "KichThuoc");
            txtMaRap.DataBindings.Add("Text", bssp, "MaRap");
            txtMaKhuon.DataBindings.Add("Text", bssp, "Makhuon");
            txtDoKho.DataBindings.Add("Text", bssp, "DoKho");
            txtHinhVe.DataBindings.Add("Text", bssp, "HinhVe");
            if (dsSanPham.Tables[0].Rows.Count > 0 && File.Exists(dsSanPham.Tables[0].Rows[0]["HinhVe"].ToString()))
            {
                picHinhVe.Image = Image.FromFile(dsSanPham.Tables[0].Rows[0]["HinhVe"].ToString());
            }
        }

        private void dgvkhachhang_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaOPhongKeHoach() == true)
                {
                    if (dgvkhachhang.CurrentRow.Cells["MaKhachHang"].Value != null
                        && !dgvkhachhang.CurrentRow.Cells["MaKhachHang"].Value.ToString().Equals(""))
                    {
                        bskh.EndEdit();
                        dgvkhachhang.EndEdit();
                        khachHangBL.updateKhachHang(dsKhachHang.Tables[0]);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnTaoMoi_Click(object sender, EventArgs e)
        {
            try
            {
                if ( QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaPhongCongNghe() == true)
                {
                bssp.EndEdit();
                if (!cmbTimKiemMaSanPham.SelectedValue.ToString().Equals(txtMaSanPham.Text))
                {
                    dtSanPham.AcceptChanges();
                    dtSanPham.Rows[0].SetAdded();
                }
                sanPhamBL.updateTimKiemSanPham(dtSanPham);
                }
                //if (!tonTaiSanPhamTrongBang(txtMaSanPham.Text))
                //{
                //    DataRow dr = dtSanPham.NewRow();
                //    dr["MaSanPham"] = txtMaSanPham.Text;
                //    dr["TenSanPham"] = txtTenSanPham.Text;
                //    dr["SoMau"] = txtSoMau.Text;
                //    dr["KichThuoc"] = txtKichThuoc.Text;
                //    dr["DoKho"] = txtDoKho.Text;
                //    dr["MaRap"] = txtDoKho.Text;
                //    dr["MaKhuon"] = txtMaKhuon.Text;
                //    dr["MaNapChup"] = txtMaNapChup.Text;
                //    dr["HinhVe"] = txtHinhVe.Text;
                //    dr.SetAdded();
                //    dtSanPham.Rows.Add(dr);
                //}                
                //sanPhamMoi = sanPhamBL.taoSanPhamMoi(txtMaSanPham.Text, txtTenSanPham.Text, txtSoMau.Text, txtKichThuoc.Text, txtDoKho.Text, txtMaRap.Text, txtMaKhuon.Text, txtMaNapChup.Text, txtHinhVe.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private Boolean tonTaiSanPhamTrongBang(string maSanPham)
        {
            if (dtSanPham != null)
            {
                foreach (DataRow item in dtSanPham.Rows)
                {
                    if (item["MaSanPham"].ToString().Equals(maSanPham))
                    {
                        return true;
                    }
                }                
            }
            return false;
        }

        private void btnChonHinhAnh_Click(object sender, EventArgs e)
        {           
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                // display image in picture box
                picHinhVe.Image = new Bitmap(open.FileName);
                // image file path
                txtHinhVe.Text = open.FileName;
            } 
        }

        private bool kiemTraCotBiNullVaRong(object obj)
        {
            try
            {
                if (obj == null || obj.ToString().Equals(""))
                {
                    return false;
                }
                return true;
            }
            catch (Exception)
            {                
                
            }
            return false;            
        }

        private void dgvDonHang_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaOPhongKeHoach())
                {
                    //if (dgvDonHang.CurrentRow.Cells["MaDonHang"].Value != null || dgvDonHang.CurrentRow.Cells["MaKhach Hang"].Value != null ||
                    //    !dgvDonHang.CurrentRow.Cells["MaDonHang"].Value.ToString().Equals("") || !dgvDonHang.CurrentRow.Cells["MaKhach Hang"].Value.ToString().Equals(""))
                    if (kiemTraCotBiNullVaRong(dgvDonHang.CurrentRow.Cells["MaDonHang"].Value) && kiemTraCotBiNullVaRong(dgvDonHang.CurrentRow.Cells["MaKhachHang1"].Value))
                    {
                        if (!dgvDonHang.CurrentRow.Cells["NgayGiaoTmp"].Value.ToString().Equals(""))
                        {
                            dgvDonHang.CurrentRow.Cells["NgayGiaoInvisible"].Value = DateTime.ParseExact(dgvDonHang.CurrentRow.Cells["NgayGiaoTmp"].Value.ToString(), "dd/MM/yyyy", null).ToString("yyyy/MM/dd");
                        }
                        xoaDongTamTrongDataTable(dsThongTinDonHang.Tables[0]);

                        dgvDonHang.CurrentRow.Cells["NgayNhanInvisible"].Value = DateTime.ParseExact(dgvDonHang.CurrentRow.Cells["NgayNhanTmp"].Value.ToString(), "dd/MM/yyyy", null).ToString("yyyy/MM/dd");
                        bsttdh.EndEdit();
                        dgvDonHang.EndEdit();

                        thongTinDonHangBL.updateThongTinDonHang(dsThongTinDonHang.Tables[0]);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void xoaDongTamTrongDataTable(DataTable dt)
        {
            if (dt != null)
            {
                for (int i = dt.Rows.Count - 1 ; i >= 0; i--)
                {
                    if (dt.Rows[i].RowState != DataRowState.Deleted && (!kiemTraCotBiNullVaRong(dt.Rows[i][0]) || !kiemTraCotBiNullVaRong(dt.Rows[i][1])))
                    {
                        dt.Rows.Remove(dt.Rows[i]);
                    }
                }
            }

        }

        private void dgvDonHangChiTiet_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaOPhongKeHoach() == true)
                {
                    bsdh.EndEdit();
                    dgvDonHangChiTiet.EndEdit();
                    donHangBL.updateDonHang(dsDonHang.Tables[0]); 
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        private void dgvDonHang_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaOPhongKeHoach())
            {
                bsttdh.EndEdit();
                dgvDonHang.EndEdit();

                thongTinDonHangBL.updateThongTinDonHang(dsThongTinDonHang.Tables[0]);
            }
        }

        private void btnBOM_Click(object sender, EventArgs e)
        {
            try
            {
                frmBOM frm = new frmBOM(cmbTimKiemMaSanPham.SelectedValue.ToString());
                frm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

        private void dgvkhachhang_RowUnshared(object sender, DataGridViewRowEventArgs e)
        {

        }
    }    
}
